//==============================================================================
//
//               Copyright (C) 2004  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2004-11-14 07:03:20 $ $Revision: 1.9 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DHTTPClient.h"

#include "DInc.h"
#include "DTest.h"

//-Collections-----------------------------------------------------------------

void DHTTPClient_test()
{
  DHTTPClient   *client = [DHTTPClient new];
  DURL          *url    = [DURL        new];
  DHashIterator *iter   = nil;
  DText         *value  = nil;
  STARTTEST();

  [url url :"http://localhost:4444/note.gif"];
  TEST([client sendStartRequest :DHC_GET :url]);
  TEST([client sendConnectionHeader :"Close"]);
  TEST([client sendUserAgentHeader :"libofc-0.4.0"]);
  TEST([client sendEndRequest]);
  TEST([client receiveReply]);
  TEST([client bodyLength] == 580);
  TEST([client reason] == 200);
  TEST([client peerMajor] == 1);
  TEST([client peerMinor] == 1);
  value = [client header :"connection"];
  TEST(value != nil);
  TEST([value icompare :"close"] == 0);
  value = [client header :"content-type"];
  TEST(value != nil);
  TEST([value icompare :"image/gif"] == 0);
  value = [client header :"unknown-header"];
  TEST(value == nil);
  
  iter = [client headers];
  
  value = [iter first];
  while (value != nil)
  {
    //printf("Header %s = %s\n", [[iter key] cstring], [value cstring]);
    
    value = [iter next];
  }
  [iter free];

  [url url :"http://localhost:4444/test.html"];
  TEST([client sendStartRequest :DHC_GET :url]);
  TEST([client sendEndRequest]);
  TEST([client receiveReply]);
  TEST([client bodyLength] == 6067);

  [url url :"http://localhost:4444/node.gif"];
  TEST([client sendStartRequest :DHC_GET :url]);
  TEST([client sendEndRequest]);
  TEST([client receiveReply]);
  TEST([client reason] == 404);
  TEST([client bodyLength] == 0);

  [url url :"http://localhost:4444/test.html"];
  TEST([client sendStartRequest :DHC_GET :url]);
  TEST([client sendConnectionHeader :"Close"]);
  TEST([client sendEndRequest]);
  TEST([client receiveReply]);
  TEST([client bodyLength] == 6067);

  [client free];
  [url    free];
  
  STOPTEST();
}
